var requestService = require('../../utils/requestService.js');
var config = require('../../libs/config.js');
var app = getApp();
Page({
  data: {
    chargerData:{
    },
    endBt:{
      disabled: false,
      loading: false
    },
  hours: 0,//时
  minuters: 0,//分
  seconds: 0//秒
  },
  onLoad: function (options) {
    // 设置本页导航标题
    wx.setNavigationBarTitle({
      title: '充电'
    })
    const address= options.address;
    const that = this;
    that.setData({
        "chargerData.address":address
    })
    wx.showLoading({
          title: '加载中',
           mask: true
    })
    that.startWebsockt(that);
  },
  onUnload:function(){
        wx.closeSocket();
  },
  /**结束充电*/
  stopCharge: function () {
    const that = this;
    const chargeSn = that.data.chargerData.chargeJrnl;
    if(chargeSn!=null && chargeSn!=''){
        that.setData({
            "endBt.disabled": true,
            "endBt.loading": true
        });
        requestService.sendPostRequest("/charger/closeCharge", { 'chargeSn': chargeSn }, function (result) {
            const resultData = result.data;
            if (resultData.resCode == 0) {
                setTimeout(() => {   that.gotoChargeBillDetail(chargeSn,that); }, 7000);
            } else {
                wx.showModal({
                    title: '提示',
                    content: resultData.message,
                    showCancel: false
                })
            }
        }, function (result) {
            wx.showModal({
                title: '提示',
                content: '失败',
                showCancel: false
            })
        })
    }else{
        wx.showModal({
            title: '提示',
            content: '网络异常请重试',
            showCancel: false
        })
    }
  },
   /**进入账单明细页面*/
   gotoChargeBillDetail:function (chargeSn,that) {
       requestService.sendGetRequest("/bill/detail/"+chargeSn, null, function (result) {
           const resultData = result.data;
           const orderNo=resultData.resData.orderNo;
           const chargerFee=that.data.chargerData.chargerFee;
           if (resultData.resCode == 0 && orderNo!=null && parseFloat(chargerFee)>0) {
               /*wx.redirectTo({
                   url: '../order/detail?orderNo='+orderNo
               })*/
               that.paymentImmediate(orderNo);
           }else{
               wx.redirectTo({
                   url: "../payment/payment_detail?param1="+orderNo
               })
           }

       })
    },
    startWebsockt:function (that) {
        //建立连接
        wx.connectSocket({
            url: config.Config.websocketAddress+"/"+app.globalData.token
        })
        //连接成功
        wx.onSocketOpen(function() {
            wx.sendSocketMessage({
                data: 'stock',
            })
        })

        //接收数据
        wx.onSocketMessage(function(res) {
            wx.hideLoading();
            var objData = JSON.parse(res.data);
            that.setData({
                chargerData:objData
            })
            that.chargerMinitesTimer();
            if(objData.nobanlance==true){
                that.setData({
                    "endBt.disabled": true,
                    "endBt.loading": true
                });
                wx.showModal({
                    title: '提示',
                    content: '余额不足，充电结束',
                    showCancel: false
                })
                const chargeSn = objData.chargeJrnl;
                wx.closeSocket();
                setTimeout(() => {   that.gotoChargeBillDetail(chargeSn); }, 7000);
            }
        })

        //连接失败
        wx.onSocketError(function() {
            console.log('websocket连接失败！');
            wx.closeSocket();
            that.startWebsockt(that);

        })

        //连接失败
        wx.onSocketClose(function() {
            console.log('websocket关闭！');
        })
    },
    /**充电时间累计*/
    chargerMinitesTimer: function () {
        // 初始化计时器
        let that = this;
        let s = that.data.chargerData.tseconds;
        let m = that.data.chargerData.tminuters;
        let h = that.data.chargerData.thours;
        that.setData({
            hours: h,
            minuters: m,
            seconds: s
        });
        // 计时开始
        const chargerMinitesTimer = setInterval(() => {
            this.setData({ seconds: s++ });
        if (s == 60) {
            s = 0;
            m++;
            setTimeout(() => { that.setData({ minuters: m }); }, 1000);
            if (m == 60) {
                m = 0;
                h++
                setTimeout(() => { that.setData({ hours: h }); }, 1000);
            }
        };
    }, 1000);
    },
    /**立即支付*/
    paymentImmediate:function (orderNo) {
        let that=this;
        requestService.sendPostRequest("/payment/ewallet/topay/"+orderNo, null, function (result) {
            const resultData = result.data;
            console.log(resultData);
            if( resultData.resCode == 0 && resultData.resData == true){
                wx.redirectTo({
                    url: "../payment/payment_detail?param1="+orderNo
                })
            }else{
                let errMsg="支付失败";
                if(resultData.message!=null){
                    errMsg=resultData.message;
                }
                console.log("errMsg--->"+errMsg);
                wx.redirectTo({
                    url: '../payment/payment_detail?param1='+orderNo+'&errMsg='+errMsg
                })
            }
        },function () {

        });

    }
})